/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package MyCondition;

import MyDataContainer.*;
import MyDataType.MyData;
import MyDataType.MyInt;
import MyDataType.MyString;
import MyDataType.MyType;
import MyDatabaseHandler.DatabaseHandler;
import MyStatementExecution.MySelect.MySelectStatement;
import StringJoinerCutter.StringJoinerCutter;
import java.util.ArrayList;

/**
 *
 * @author Welcome
 */
public class ConditionHandler {
    private static String leftSide;
    private static String rightSide;
    private static String signString;
    private static String[] listSign={"=",">","<",">=","<=","like","in"};
    
    /**
     * Trả về mảng các vị trí row thỏa điều kiện, tìm đc rồi thích làm gì với nó thì làm :D
     * Lấy chỉ số để select (lọc lại query), delete (xóa dòng), updata...
     * @param targetQuery
     * @param conString
     * @return 
     */
    public static ArrayList<Integer> getRowNumberMeetCondition(MyQuery targetQuery,String conString,DatabaseHandler databaseHandler)
    {
        //luu thong tin cac cot thoa dieu kien
        ArrayList<Integer> resultIntList = new ArrayList<Integer>();
        ConditionType kind=ConditionType.ATT_ATT;
        
        signString = findSignString(conString);
        
        if(!signString.equals(""))
        {
            String[] temp = conString.split(signString);
            leftSide = temp[0].trim();
            rightSide = temp[1].trim();
        }
        
        if(rightSide.indexOf("(")>-1 ||signString.equals("in"))
            kind=ConditionType.TUP_QUE;
        else
        {
            int left=-1;
            for(int i=0; i<targetQuery.getColumnTitles().getSize();i++)
            {
                left  = targetQuery.getColumnTitles().getAt(i).toLowerCase().indexOf(leftSide);
                if(left >-1)
                    break;
            }
            
            int right=-1;
            for(int i=0; i<targetQuery.getColumnTitles().getSize();i++)
            {
                right  = targetQuery.getColumnTitles().getAt(i).toLowerCase().indexOf(rightSide);
                if(right >-1)
                    break;
            }
            
            if(left>-1 && right>-1)
                kind=ConditionType.ATT_ATT;
            else
                kind=ConditionType.ATT_PAT;
        }
        
       
        switch (kind)
        {
            case ATT_ATT:
                resultIntList = processForATT_ATT(targetQuery);
                break;
            case ATT_PAT:
                resultIntList = processForATT_PAT(targetQuery);
                break;
            case TUP_QUE:
                resultIntList = processForTUP_QUE(targetQuery,databaseHandler);
                break;
        }

        return resultIntList;
    }

    private static String findSignString(String conString) {
        String result="";
        
        //ArrayList<Integer> intList = new ArrayList<Integer>();
        
        int temp=99999;
        for(int i=0; i<listSign.length;i++)
        {
            int xxx = conString.indexOf(listSign[i]);
            if(xxx>-1 && xxx<temp)
            {
                result = listSign[i];
                temp = xxx;
            }
        }
        
        return result;
    }

    private static ArrayList<Integer> processForATT_ATT(MyQuery targetQuery) {
        ArrayList<Integer> result = new ArrayList<Integer>();
        int column1 = targetQuery.getColumnTitles().getPosition(leftSide);
        int column2 = targetQuery.getColumnTitles().getPosition(rightSide);
        
        for(int i=0; i<targetQuery.getSize();i++)
        {
            MyDataSet rowDataSet = targetQuery.getRowAt(i);
            MyData leftData = rowDataSet.getDataAt(column1);
            MyData rightData = rowDataSet.getDataAt(column2);
            boolean temp = leftData.comparisonWith(rightData, signString);
            if(temp==true)
                result.add(i);
        }
        
        return result;
    }

    private static ArrayList<Integer> processForATT_PAT(MyQuery targetQuery) {
        ArrayList<Integer> result = new ArrayList<Integer>();
        
        int columnPos = targetQuery.getColumnTitles().getPosition(leftSide);
        for(int i=0; i<targetQuery.getSize();i++)
        {
            MyDataSet rowDataSet = targetQuery.getRowAt(i);
            MyData leftData = rowDataSet.getDataAt(columnPos);
            MyData rightData=null;
            MyType type = leftData.getType();
            //Mai lam tip tu day
            switch(type)
            {
                case STRING:
                    rightData = new MyString(rightSide);
                    break;
                case INT:
                    rightData = new MyInt(rightSide);
                    break;
                case DATE:
                    break;
            }
            
            boolean temp = leftData.comparisonWith(rightData, signString);
            if(temp)
                result.add(i);
        }
        
        return result;
    }

    private static ArrayList<Integer> processForTUP_QUE(MyQuery targetQuery,DatabaseHandler databaseHandler) {
        ArrayList<Integer> result = new ArrayList<Integer>();
        
        int columnPos = targetQuery.getColumnTitles().getPosition(leftSide);
        MyDataSet rightSet = new MyDataSet();
        
        if(rightSide.indexOf("select")==-1)
        {
            //vd: madt in ('100','101,'102')
            String dataType = targetQuery.getDataTypeOfColumn(leftSide);
            char x = dataType.charAt(0);
            switch(x)
            {
                case 'i':
                    rightSet.setData(rightSide, MyType.INT);
                    break;
                case 's':
                    rightSet.setData(rightSide, MyType.STRING);
                    break;
                    
            }
            
            for(int i=0; i<targetQuery.getSize();i++)
            {
                MyDataSet rowDataSet = targetQuery.getRowAt(i);
                MyData leftData = rowDataSet.getDataAt(columnPos);
                if(rightSet.isIN(leftData)==true)
                    result.add(i);
            }

        }
        
        else
        {
            //rightSide la mot cau select
            String newTempSelect = rightSide.substring(1, rightSide.length()-1).trim();
            MySelectStatement selectStatement = new MySelectStatement(databaseHandler, newTempSelect);
            
            MyQuery tempQuery = new MyQuery(selectStatement.executeSelectStatement());
            
            rightSet = tempQuery.getColumnAt(0);
            
            for(int i=0; i<targetQuery.getSize();i++)
            {
                MyDataSet rowDataSet = targetQuery.getRowAt(i);
                MyData leftData = rowDataSet.getDataAt(columnPos);
                
                if(rightSet.isIN(leftData)==true)
                    result.add(i);
            }
            
        }
                        
        return result;
    }
}
